package com.zleetcode.链表;

/**
 * https://leetcode-cn.com/problems/linked-list-cycle/
 */
public class _141_环形链表 {
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        // 快指针指向下一个节点
        ListNode fast = head.next;
        // fast走的快，所以while中判断fast
        while (fast != null && fast.next != null) {
            if (slow.val == fast.val) {
                return true;
            }
            // 走一步
            slow = slow.next;
            // 走两步
            fast = fast.next.next;
        }
        return false;
    }
}
